package com.goods.controller.business;

import com.goods.business.service.BizHealthService;
import com.goods.common.model.system.User;
import com.goods.common.response.ResponseBean;
import com.goods.common.utils.JWTUtils;
import com.goods.common.vo.business.HealthVO;
import com.goods.common.vo.system.PageVO;
import com.goods.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Locale;
import java.util.Map;

@RestController
@RequestMapping("/business/health")
public class BizHealthController {

    @Autowired
    private BizHealthService bizHealthService;

    @Autowired
    private UserService userService;

    @GetMapping("/isReport")
    public ResponseBean checkIsReport(HttpServletRequest request) {
        User user = this.getUser(request);
        //根据userId查询bizhealth表是否存在记录
        HealthVO healthVO = bizHealthService.checkIsReport(user);
        return ResponseBean.success(healthVO);
    }

    @PostMapping("/report")
    public ResponseBean reportHealthStatus(@RequestBody HealthVO healthVO, HttpServletRequest request) {
        //获取用户名
        User user = this.getUser(request);
        bizHealthService.saveUserHealthStatus(healthVO,user);
        return ResponseBean.success();
    }

    @GetMapping("/history")
    public ResponseBean getHistory(@RequestParam Map<String,Object> queryMap, HttpServletRequest request){
        User user = this.getUser(request);
        PageVO<HealthVO> pageVO = bizHealthService.getHistoryByUser(queryMap,user);
        return ResponseBean.success(pageVO);
    }

    private User getUser(HttpServletRequest request){
        //获取token
        String token = request.getHeader("Authorization");
        //根据token获取用户名
        String username = JWTUtils.getUsername(token);
        //根据用户名查询是否存在该用户
       return userService.findUserByName(username);
    }

}
